/**
 * @description  : 大案快报列表
 * @descriptionDetail : 大案快报列表
 * @copyright    : 浙江烟草
 * @author       : mc
 * @create       : 2022-12-05 15:51:03
 */
<template>
  <view>
    <!-- #ifdef H5-ONLY -->
    <theme-navbar :is-white="false" :title="navTitle" :custom-back="goBack" style="z-index: 99999">
    </theme-navbar>
    <!-- #endif -->
    <view class="nolicence-search-lay">
      <view class="key-search">
        <u-search
          bg-color="#fafafa"
          shape="round"
          :clearabled="true"
          :showAction="true"
          :animation="true"
          actionText="确定"
          placeholder="案件编号" 
          v-model="searchKey"
          @custom="toSearchList"
        ></u-search>
      </view>
    </view>
    <view class="content u-page">
      <list-view
        :i="0" 
        :index="0" 
        :load-data="loadData"
        :top="102"
        :bottom="getListViewBottom()"
        :init-load="false" 
        ref="mescrollItem0"
      >
        <!-- :top= "getListViewTop()" -->
        <view slot-scope="d" class="list-item-root">
          <u-swipe-action
            :show="d.data.show"
            :index="d.data.bizUuid" 
            :disabled="d.data.swiperDisabled"
            :btn-width="160"
            @click="swiperClick"
            @open="swiperOpen"
            :options="d.data.swiperOptions"
          >
            <view class="list-lay">
              <list-item-major-case-report :itemInfo="d.data"></list-item-major-case-report>
            </view>
          </u-swipe-action>
        </view>
      </list-view>
      <view class="submit-btns btn-other-cls">
        <u-button class="del" type="primary" @click="bindCase()">
          已立案上报
        </u-button>
        <u-button plain class="mit" type="primary" @click="unBindCase()">
          未立案上报
        </u-button>
      </view>
    </view>
  </view>
</template>

<script>
import MescrollMoreMixin from "@/components/mescroll-uni/mixins/mescroll-more.js";
import ListView from "@/components/list_view.vue";
import ListItemMajorCaseReport from "./components/listItemsMajorCaseReport.vue";
import bigCaseService from "@/service/case/bigCase.service.js";
import commonService from "@/service/common/common.service.js";
import {uuid,} from "@/common/util.js";
export default {
  components: {
    ListView,
    ListItemMajorCaseReport,
  },
  mixins: [MescrollMoreMixin,],
  data() {
    return {
      searchKey:"",
      popupHeight:"360rpx",
      tmpItem:{},
      isStatus:false,
      user: this.$store.state.userInfo,
      preParams:{},
    }
  },
  onLoad() {
    this.preParams = this.$u.deepClone(this.getRouteParam() || {});
  },
  mounted() {
    // this.setRightBtn();
  },
  onShow(){
    if(this.isStatus){
      this.getMescroll(0).triggerDownScroll();
      this.isStatus = false;
    }
  },
  methods: {
    getListViewTop() {
      let p = uni.upx2px(64) + uni.upx2px(10);
      return p  + "px";
    },
    getListViewBottom(){
      return uni.upx2px(140) + 10;
    },
    toSearchList(e){
      this.getMescroll(0).triggerDownScroll();
    },
    // 用的工作待办接口筛选数据，不用做分页查询，默认多查一批数据出来
    loadData(page) {
      return new Promise((resolve, reject) => {
        let par = {
          page: page.num,
          limit: page.size,
          paras:{
            caseCode: this.searchKey,
            isMine:1,
            orgUuid:this.user.user.org.orgUuid,
          },
        };
        if(this.preParams.bizUuid){
          par.paras.applyUuid = this.preParams.bizUuid;
        }
        bigCaseService.bigCaseApplyPage(par).then(res => {
          if(res.success){
            res.data.forEach(itm => {
              this.getItemCanEdit(itm);
              itm.show = false;
            });
            resolve({
              data:{
                totalCount: res.count,
                list:res.data,
              },
            });
          }else{
            this.$u.toast(res.msg);
            resolve({
              data:{
                totalCount:0,
                list:[],
              },
            });
          }
        }).catch(e => {
          this.$u.toast('请求失败:' + e);
          resolve({
            data:{
              totalCount:0,
              list:[],
            },
          });
        });
      });
    },
    swiperClick(index1, index){
      let datas = this.$refs.mescrollItem0.listDatas;
      let cur = -1;
      for (let i = 0; i < datas.length; i++) {
        if(datas[i].bizUuid == index1){
          cur = i;
          break;
        }
      }
      if(cur == -1){
        this.$u.toast("选中数据无效");
        return;
      }
      let tmpDt = datas[cur].swiperOptions;
      if(tmpDt[index].action == 'del'){ // 删除
        // TODO 先异步删除，在promise中的删除后再删除listDatas 数据
        // datas.splice(cur, 1);
        this.$modal.confirm(`当前记录的状态为【${datas[cur].stageName}】,是否需要删除当前记录。`,
          "提示", () => {
            this.$modal.showLoading('正在删除...');
            bigCaseService.delBigCaseApply({applyUuid: datas[cur].applyUuid,}).then(res => {
              this.$modal.hideLoading();
              if(res.success){
                this.$u.toast('删除成功');
                this.getMescroll(0).triggerDownScroll();
              }else{
                this.$u.toast(res.msg);
              }
            }).catch(e => {
              this.$modal.hideLoading();
            });
          });
      }else if(tmpDt[index].action == 'edit'){ // 修改
        this.isStatus = true;
        this.setRouteParam(datas[cur]);
        this.$u.route({
          url:"/pages/majorCaseReport/majorCaseReportEdit",
          params:{
            from: 'edit',
          },
        });
      }else if(tmpDt[index].action == 'association'){ // 关联案件
        this.tmpItem = datas[cur];
        this.$u.route({
          url:"pages/majorCaseReport/components/caseSelection",
          params:{
            from:'association',
          },
        });
      }
      this.$refs.mescrollItem0.listDatas.map((val, idx) => {
        this.$refs.mescrollItem0.listDatas[idx].show = false;
      });
    },
    bindCase(){
      this.isStatus = true;
      this.$u.route({
        url:"pages/majorCaseReport/components/caseSelection",
        params:{
          from:'add',
        },
      });
    },
    unBindCase(){
      this.initMainFlow({caseUuid: '',});
    },
    updateBackState(p){
      if(p.selectCase){
        if(p.selectCase.from == 'add'){
          this.initMainFlow({
            caseUuid: p.selectCase.data.caseUuid,
            caseType: p.selectCase.data.caseType,
          });
        }else if(p.selectCase.from == 'association'){
          this.tmpItem.caseUuid = p.selectCase.data.caseUuid;
          this.tmpItem.caseCode = p.selectCase.data.caseCode;
          this.tmpItem.caseType = p.selectCase.data.caseType;
          if(this.preParams.taskId){
            this.tmpItem.taskId = this.preParams.taskId;
          }
          let tmpParam = this.$u.deepClone(this.tmpItem);
          try{
            delete tmpParam.swiperOptions;
          }catch(e){
            console.log(e);
          }
          this.$modal.showLoading('正在关联...');
          bigCaseService.updateById(tmpParam).then(res => {
            this.$modal.hideLoading();
            if (res.success) {
              this.$u.toast("成功关联");
              this.getMescroll(0).triggerDownScroll();
            } else {
              this.$u.toast(res.msg);
            }
          }).catch(e => {
            this.$modal.hideLoading();
          })
        }
        
      }else if(p.refresh){
        // this.getMescroll(0).triggerDownScroll();
      }
    },
    /**
	 * 实例化主流程
	 */
    initMainFlow(config){
      let caseUuid = config.caseUuid || '';
      let bizUuid = uuid();
      let param = {
        bizUuid: bizUuid,
        docCode: 'docBigCaseApplyReport',
        typeCode: 'dakb',
        docType: 'case',
        itemCode: 'dakbsb',
      };
      this.$modal.showLoading('初始化中...');
      commonService.initOtherMain(param).then(res => {
        if(res.success){
          let bean = res.bean;
          let p = {
            docUuid: bean.docConfig.docUuid,
            titemUuid: bean.titemUuid,
            bizUuid: bizUuid,
            docType: 'case',
            caseType: config.caseType,
            caseUuid: caseUuid,
          };
          bigCaseService.canAdd({
            bizUuid: bizUuid,
            docUuid: bean.docConfig.docUuid,
          }).then(res1 => {
            this.$modal.hideLoading();
            if(res1.success){
              this.setRouteParam(p);
              this.isStatus = true;
              this.$u.route({
                url:"pages/majorCaseReport/majorCaseReportEdit",
                params:{
                  from:'add',
                },
              });
            }else{
              this.$u.toast(res1.msg);
            }
          }).catch(e => {
            console.log(e);
            this.$modal.hideLoading();
            this.$u.toast("请求异常：" + e);
          });
        }else{
          this.$u.toast(res.msg);
        }
      }).catch(e => {
        this.$modal.hideLoading();
        this.$u.toast('初始化失败:' + e);
      });
    },
    swiperOpen(index){
      this.$refs.mescrollItem0.listDatas.map((val, idx) => {
        this.$refs.mescrollItem0.listDatas[idx].show = index == val.bizUuid;
      });
    },
    getItemCanEdit(item){
      // && item.stage == 'cg';
      let disabled = true;
      let swiperOptions = [];
      if(item.flag == '1'){
        if(this.$u.test.isEmpty(item.caseUuid) && item.stage != 'cg'){
          if(this.$u.test.isEmpty(item.caseCode)){
            disabled = false;
            swiperOptions.push({
              text: '关联案件',
              action:"association",
              style: {
                backgroundColor: this.$theme.u_type_success,
              },
            });
          }
        }
        if(item.stage == 'cg'){
          disabled = false;
          swiperOptions.push({
            text: '编辑',
            action:"edit",
            style: {
              backgroundColor: this.$theme.u_type_primary,
            },
          });
          swiperOptions.push({
            text: '删除',
            action:"del",
            style: {
              backgroundColor: this.$theme.u_type_error,
            },
          });
        }
      }
      item.swiperOptions = swiperOptions;
      item.swiperDisabled = disabled;
    },
  },
}
</script>

<style scoped lang="scss">
	@import "@/style/common/rightMenu.scss";
	.content {
		background-color: #F3F4F5 ;
    padding:3px 0px;
	}
	.key-search{
		width: 100%;
		position: fixed;
		// 
		background-color: white;
		z-index: 99;
		padding: $cm-search-padding-row $cm-search-padding-column;
		//#ifdef H5-DINGDING
		top: 0;
		//#endif
		//#ifndef H5-DINGDING
		top: 44px;
		//#endif
	}
	.list-item-root{
		margin: 10px;
    border-radius: 15px;
    overflow: hidden;
	}
	.list-lay{
    padding: 10px;
	}
	// 页面底部按钮样式
	.submit-btns {
		display: flex;
		position: fixed;
		bottom: 40rpx;
		padding-left: 40rpx;
		padding-right: 40rpx;
		width: 100%;
		text-align: center;
		z-index: 99;
	
		.del {
			flex: 1;
		}
	
		.mit {
			flex: 1;
			margin-left: 40rpx;
		}
	}
	
	.btn-other-cls {
		padding-top: 20rpx;
		position: fixed;
		bottom: 0;
		padding-bottom: 20rpx;
		background-color: white;
		border-top: 1px solid $u-border-color;
	}
 
</style>
